package com.example.demo.controller;

import java.util.concurrent.atomic.AtomicLong;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestExceptionInvodeController {
  @Autowired
  private UserService userService;
  
  private AtomicLong atomicLong = new AtomicLong(1L);
  
  @RequestMapping({"/queryException/{id}"})
  @ResponseBody
  public User throwException(@PathVariable String id) throws Exception {
    long currentIndex = this.atomicLong.addAndGet(1L) % 3L;
    if (currentIndex <= 1L)
      throw new Exception("this is a custom test Exception"); 
    if (currentIndex <= 2L) {
      this.userService.npeInvoke();
    } else {
      this.userService.exceptionFromAccountServiceInvoke();
    } 
    return null;
  }
}